from enum import Enum

class Direction(Enum):
    FRONT = 0  # 前
    LEFT = 1   # 左
    BACK = 2   # 后
    RIGHT = 3  # 右
    
    @property
    def direction_name(self):
        """获取方向名称"""
        names = {
            0: "前",
            1: "左",
            2: "后",
            3: "右"
        }
        return names[self.value]
    
    def __add__(self, num):
        """加法操作，结果保持在0-3范围内"""
        new_value = (self.value + num) % 4
        return Direction(new_value)
    
    def __sub__(self, num):
        """减法操作，结果保持在0-3范围内"""
        new_value = (self.value - num) % 4
        return Direction(new_value)
    
    def __str__(self):
        return f"{self.value}({self.direction_name})"
    
    def __mod__(self, num):
        return self.__add__(num)  # 支持 % 运算符
    
    @classmethod
    def from_number(cls, num):
        """从数字创建枚举值"""
        num = num % 4
        return cls(num)
    
    @classmethod
    def from_name(cls, name):
        """从方向名称创建枚举值"""
        name_map = {
            "前": cls.FRONT,
            "左": cls.LEFT,
            "后": cls.BACK,
            "右": cls.RIGHT
        }
        return name_map.get(name, cls.FRONT)  # 默认返回FRONT
    
if __name__ == "__main__":
            # 创建枚举实例
    front = Direction.FRONT
    left = Direction.LEFT

    # 获取值和方向名
    print(front.value)          # 输出: 0
    print(front.direction_name) # 输出: "前"

    # 数字加减操作
    new_dir = front + 1
    print(new_dir)              # 输出: 1(左)

    new_dir = left + 2
    print(new_dir)              # 输出: 3(右)

    new_dir = Direction.RIGHT - 2
    print(new_dir)              # 输出: 1(左)

    # 从数字创建
    dir_from_num = Direction.from_number(2)
    print(dir_from_num)         # 输出: 2(后)

    # 从名称创建
    dir_from_name = Direction.from_name("右")
    print(dir_from_name)        # 输出: 3(右)